export default {
	data() {
		return {
			//设置默认的分享参数
			user: null,
		}
	},
	onShareAppMessage(res) { //发送给朋友
		setTimeout(() => {
			console.log('混入', this.$store.state.shareParams);
		}, 500)
		let pid = uni.getStorageSync('user').UserID;
		let Name = uni.getStorageSync('user').Name;
		let userName = this.$util.entitiesToUtf16(Name)
		return {
			title: `${userName}向您推荐了LoveMoment，赶快来吧`,
			path: `/pages/tab/home?pid=${pid}`,
			query: `pid=${pid}`
			//imageUrl: 'https://golfdate.oss-cn-shenzhen.aliyuncs.com/share-bg.png'
		}
	},
	async onLoad(e) {
		this.submitPath()
	},
	onShow() {
		
	},
	computed: {
		// 获取时间(好像没有用)
		getTime() {
			return l => {
				let d = new Date(l);
				let timestamp = d.getTime();
				let d2 = new Date();
				let timestamp2 = d2.getTime();
				if (timestamp2 - timestamp < (30 * 24 * 60 * 60 * 1000)) {
					return this.$u.timeFrom(timestamp, 'mm-dd hh:MM');
				} else {
					return this.$u.date(timestamp, 'mm-dd hh:MM');
				}



			}
		},
		// 获取图片
		getImg(url) {
			return l => {
				try {
					let arr = JSON.parse(l);
					if (Array.isArray(arr)) {
						return arr[0];
					}
				} catch (error) {
					return '';
				}
			}
		}
	},

	methods: {
		async getMyCartNum() {
			let res = await this.$api.post('ShopCart/MyCart', {
			})
			if (res.code == 0) {
				let shoplength = 0;
				for (let i = 0; i < res.data.length; i++) {
					for (let j = 0; j < res.data[i].Item.length; j++) {
						shoplength += 1
					}

				}
				shoplength = String(shoplength)
				if (shoplength == '0') {
					uni.removeTabBarBadge({ //隐藏数字标
						index: 2 //tabbar下标
					})
				}else{
					uni.setTabBarBadge({ //显示数字  
						index: 2, //tabbar下标
						text: shoplength //此处一定要为字符串
					})
				}
				
			}
		},
		// 控制星星回显
		starRateView(num) {
			let staring = 'https://wavestation.oss-cn-shenzhen.aliyuncs.com/upload/1719814859207878.png'
			let stared = 'https://wavestation.oss-cn-shenzhen.aliyuncs.com/upload/17202602010231341.png'
			let list = []
			for (let i = 1; i <= 5; i++) {
				let url = i <= num ? staring : stared
				list.push(url)
			}
			return list
		},
		// 返回星期
		getWeek(l) {
			let d = new Date(l);
			let week = d.getDay();
			let weekList = ['星期天', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
			return weekList[week];
		},
		//提交页面路径，记录页面被访问次数
		submitPath() {

			try {
				let user = uni.getStorageSync('user')
				let token = uni.getStorageSync('xj_token') || ''
				if (user.UserID && token != '') {
					let pages = getCurrentPages()
					const currentPage = pages[pages.length - 1];
					this.$api.post('/Common/Add_ViewPath', {
						Path: currentPage.route,
						Platform: uni.getStorageSync('platform') || 'MP-WEIXIN'
					});
				}

			} catch (error) {

			}

		},
		async checkUserAuth() {

			let user_cache = await this.$api.post('/user/GetUserInfo', {
				ID: uni.getStorageSync('user').UserID
			});

			if (!user_cache.data)
				this.nav_to('/pages/start/start1');

			if (user_cache.data.IsAuth == 0 && !user_cache.data.UserData_Error) {
				this.nav_to('/pages/start/start1');
			} else if (user_cache.data.IsAuth == 0 && user_cache.data.UserData_Error == 1) {
				uni.showModal({
					title: '头像违规',
					content: '根据平台审核规定,你的头像不符合平台要求,请上传本人照片',
					confirmText: "重新上传",
					showCancel: false,
					success: async function (res) {
						if (res.confirm) {
							uni.navigateTo({
								url: '/pages/album/modify'
							});
						} else if (res.cancel) {

						}
					}
				});
			} else if (user_cache.data.IsAuth == 0 && user_cache.data.UserData_Error == 2) {
				uni.showModal({
					title: '昵称违规',
					content: '根据平台审核规定,您的昵称不符合平台要求,不能为电话号码等敏感词',
					confirmText: "重新填写",
					showCancel: false,
					success: async function (res) {
						if (res.confirm) {
							uni.navigateTo({
								url: '/pages/album/modify'
							});
						} else if (res.cancel) {

						}
					}
				});
			}

		},
		async playScoreAddAudio(id) {
			//this.innerAudioContext.stop();//暂停音频
			const innerAudioContext = uni.createInnerAudioContext();
			innerAudioContext.src = "https://golfdate.oss-cn-shenzhen.aliyuncs.com/mp3/corn_add.mp3";//指定音频地址
			innerAudioContext.play();//播放音频

		},
		// 获取已过期用户
		async getIsExpiredUser() {
			let res = await this.$api.post('User/IsExpiredUser');
			return res.data;
		},
		// 
		toast(str) {
			// this.$refs.toast.msg(str)
			// this.$refs[toast]
			this.$refs['toast'].msg(str)
		},
		// 获取微信登录code
		async login_bind2(page) {
			let _this = this;
			uni.login({
				provider: 'weixin',
				success: res => {
					_this.wx_login2(res.code, page);
				},
				fail: err => {
					uni.showModal({
						content: '登录失败',
						showCancel: false
					});
				}
			});
		},
		// 微信小程序登录相关处理
		async wx_login2(list, page) {
			// 同步获取本地token
			let token = uni.getStorageSync('xj_token');
			// 判断是否有token
			if (token) {
				// 同步获取本地pid
				let pid = uni.getStorageSync('pid');
				// 请求获取用户微信登录相关信息
				let res = await this.$api.post('User/WXLogin', {
					Code: list,
					pid: pid,
					CompanyID: 6002
				});
				// 同步设置本地token
				uni.setStorageSync('xj_token', res.data.Token);
				// 获取用户微信小程序相关信息
				let uRes = await this.$api.post('User/GetUserInfo', {
					ID: res.data.UserID
				});
				// 用户信息
				let user = uRes.data;
				// 用户信息添加用户ID
				user.UserID = res.data.UserID;
				// 用户信息添加Role
				user.Role = res.data.Role;
				// 同步本地添加用户信息
				uni.setStorageSync('user', user);
				//状态管理添加用户信息
				this.$store.commit('setUser', user);
				// 状态管理登录状态改为true
				this.$store.commit('login', true);
				// 触发全局更新图片事件(好像没有用)
				uni.$emit('up_date_img');
			}
		},
		// 跳转页面
		nav_to(url, url2) {
			console.log('nav_to url:' + url)
			uni.navigateTo({
				url: url,
				fail: function (err) {
					if (url2) {
						uni.navigateTo({
							url: url2
						})
					} else {
						uni.switchTab({
							url: url
						})
					}
				}
			})
		},
		tab_to(url, url2) {

			uni.switchTab({
				url: url
			})
		},
		redirect_to(url) {
			uni.redirectTo({
				url: url
			})
		},
		reLaunch_to(url) {
			uni.reLaunch({
				url: url
			});
		},
		back_to() {
			uni.navigateBack({
				delta: 1
			})
		},
		// 添加约球的相关处理
		async ran_to_add_ball(url) {

			// 获取用户是否为会员
			let checkSub = await this.$api.post('Member/IsPublish');
			let _this = this;

			let user_cache = await this.$api.post('/user/GetUserInfo', {
				ID: uni.getStorageSync('user').UserID
			});

			// uni.setStorageSync('user',user_cache.data);
			// 实名认证
			if (checkSub.data.IsTrueName == -1) {
				uni.showModal({
					title: '温馨提示',
					content: checkSub.message,
					confirmText: "去认证",
					success: function (res) {
						if (res.confirm) {
							_this.nav_to('/pages/user/real_name');
						} else if (res.cancel) {

						}
					}
				});
				return false;
			} else if (user_cache.data.IsAuth != 1 && user_cache.data.UserData_Error == null) {   //没有注册
				uni.showModal({
					title: '温馨提示',
					content: '你还未注册，请先去注册',
					confirmText: "去注册",
					success: function (res) {
						if (res.confirm) {
							_this.nav_to('/pages/start/start1');
						} else if (res.cancel) {

						}
					}
				});
				return false;
			} else {
				// 跳转页面
				this.nav_to(url)
			}

		},
		// 关闭当前页面跳转指定页面
		rnav_to(url) {
			uni.redirectTo({
				url: url
			})
		},
		// 还回上一页
		nav_back(num) {
			// 判断是否传参
			if (num > 0) {
				// 延迟返回上一页
				setTimeout(() => {
					uni.navigateBack({
						delta: 1,
						fail: function () {
							// 如果错误跳转页约球tab页面
							uni.switchTab({
								url: '/pages/tab/home'
							});
						}
					})
				}, num)

			} else {
				// 直接返回上一页
				uni.navigateBack({
					delta: 1,
					fail: function () {
						uni.switchTab({
							url: '/pages/tab/home'
						});
					}
				})
			}
		},
		// 获取年的字符串
		getYearStr(dateStr) {
			if (dateStr == null || dateStr == '') {
				return '未知'
			}
			let d = new Date(dateStr);
			let year = parseInt(d.getFullYear());
			let gwei = year % 10;
			if (gwei >= 5) {
				gwei = 5;
			} else {
				gwei = 0
			}
			let shiwei = parseInt(year % 100 / 10) * 10;
			if (shiwei == 0) {
				return shiwei.toString() + gwei.toString();
			} else {
				return shiwei + gwei
			}

		},
		// 预览图片
		previewImage(url) {
			uni.previewImage({
				current: 0,
				urls: [url],
				success() {
				},
				fail(err) {
				}
			});
		},
		// 判断手机号码是否正确
		isPhone(number) {
			var reg = /^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|17[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9]|19[0|1|2|3|5|6|7|8|9])\d{8}$/;
			if (number.length == 11) {

				if (reg.test(number)) {
					return true;
				} else {
					return false;
				}
			} else {
				return false
			}

		},
		isArray(arr) {
			let isArr = Array.isArray(arr)
			if (isArr && arr.length) {
				return true
			}
			return false
		},
		//星座计算
		constellate(dateStr) {

			let date = new Date(dateStr);
			let y = date.getFullYear();
			let m = date.getMonth() + 1;
			let d = date.getDate();

			let constellations = [{
				"Start": 121,
				"End": 220,
				"Name": "水瓶座",
				"icon": "https://golfdate.oss-cn-shenzhen.aliyuncs.com/icon/xz/sp.png"
			},
			{
				"Start": 221,
				"End": 320,
				"Name": "双鱼座",
				"icon": "https://golfdate.oss-cn-shenzhen.aliyuncs.com/icon/xz/sy.png"
			},
			{
				"Start": 321,
				"End": 420,
				"Name": "白羊座",
				"icon": "https://golfdate.oss-cn-shenzhen.aliyuncs.com/icon/xz/by.png"
			},
			{
				"Start": 421,
				"End": 520,
				"Name": "金牛座",
				"icon": "https://golfdate.oss-cn-shenzhen.aliyuncs.com/icon/xz/jn.png"
			},
			{
				"Start": 521,
				"End": 620,
				"Name": "双子座",
				"icon": "https://golfdate.oss-cn-shenzhen.aliyuncs.com/icon/xz/sz.png"
			},
			{
				"Start": 621,
				"End": 720,
				"Name": "巨蟹座",
				"icon": "https://golfdate.oss-cn-shenzhen.aliyuncs.com/icon/xz/jx.png"
			},
			{
				"Start": 721,
				"End": 820,
				"Name": "狮子座",
				"icon": "https://golfdate.oss-cn-shenzhen.aliyuncs.com/icon/xz/ssz.png"
			},
			{
				"Start": 821,
				"End": 920,
				"Name": "处女座",
				"icon": "https://golfdate.oss-cn-shenzhen.aliyuncs.com/icon/xz/cn.png"
			},
			{
				"Start": 921,
				"End": 1020,
				"Name": "天秤座",
				"icon": "https://golfdate.oss-cn-shenzhen.aliyuncs.com/icon/xz/tp.png"
			},
			{
				"Start": 1021,
				"End": 1120,
				"Name": "天蝎座",
				"icon": "https://golfdate.oss-cn-shenzhen.aliyuncs.com/icon/xz/tx.png"
			},
			{
				"Start": 1121,
				"End": 1220,
				"Name": "射手座",
				"icon": "https://golfdate.oss-cn-shenzhen.aliyuncs.com/icon/xz/ss.png"
			},
			{
				"Start": 1221,
				"End": 120,
				"Name": "摩羯座",
				"icon": "https://golfdate.oss-cn-shenzhen.aliyuncs.com/icon/xz/tx.png"
			}
			];

			let daysInMonth = [31, 99, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

			//检测年份
			if (y < 1970 || y > 2099) return null;

			//检测月份
			if (m < 1 || m > 12) return null;

			//检测日期
			let mDays = daysInMonth[m - 1];
			//如果是二月，要根据年份计算天数，不是二月时略过此计算
			if (m == 2) {
				mDays = y => {
					if (y % 400 == 0 || (y % 4 == 0 && y % 100 != 0)) return 29;
					return 28;
				}
			}

			//判断日数据是不是在月份的有效天范围
			if (d < 0 || d > mDays) return "--";


			let pos = m * 100 + d;

			for (let i in constellations) {
				if (pos >= constellations[i].Start && pos <= constellations[i].End) {



					return constellations[i];
				}
			}
		}
	}
}